var core = require('../../core/enocean.js');
var fs = require('fs');
var util = require('../../core/module.js');

var config = new util.ConfigModule('morning'); // configuration file

var getMorningConf = function (req, res) {

	//TODO : take id into account
	var id = req.params.id;
	config.get(id, function (error, conf) {
		if (error) {
			res.json(error);
		} else {
			res.json(conf);
		}
	});
};

var putMorningConf = function (req, res) {

	//TODO : take id into account
	var id = req.params.id;
	var resJson = new Object();
	resJson.coffee_min = req.body.coffee_min;
	resJson.coffee_hour = req.body.coffee_hour;
	resJson.gcalendar_min = req.body.gcalendar_min;
	resJson.gcalendar_hour = req.body.gcalendar_hour;
		
	if (resJson.coffee_min == undefined || resJson.coffee_hour == undefined
		|| resJson.gcalendar_min == undefined || resJson.gcalendar_hour == undefined) {
		// TODO raise exception
		resJson.error = "Paramètres manquants";
		res.end(JSON.stringify (resJson));
	} else {
	
		config.set(id, resJson, function (error) {
			res.json( {'code':'OK'} );
		});
	}
};

module.exports.getMorningConf = getMorningConf;
module.exports.putMorningConf = putMorningConf;
